package dgirard.radar;

import android.content.Context;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RadioGroup;

public class SpeedSelector extends LinearLayout {
	
	private SpeedSelectorListener speedSelectorListener;
	
	private Context _context = null;

	public SpeedSelector(Context context) {
		super(context);
		_context = context;
		
        RadioGroup radioGroup = new RadioGroup(context);
        addRadioButton(radioGroup, 50, R.drawable.fiftyred, R.drawable.fiftygreen);
        addRadioButton(radioGroup, 90, R.drawable.nightyred, R.drawable.nightygreen);
        addRadioButton(radioGroup, 110, R.drawable.hundredtenred, R.drawable.hundredtengreen);
        addRadioButton(radioGroup, 130, R.drawable.hundredthrirtyred, R.drawable.hundredthrirtygreen);
        radioGroup.setVisibility(View.VISIBLE);
        this.addView(radioGroup);
	}
	
	private void addRadioButton(RadioGroup radioGroup, final int speed, final int idRed, final int idGreen) {
		final android.widget.RadioButton rd = new android.widget.RadioButton(_context);
        
        rd.setVisibility(View.VISIBLE);
		rd.setButtonDrawable(idGreen);
		rd.setWidth(50);
        rd.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
        	
        	final int speedRadioButton  = speed;
        	
        	 public void onCheckedChanged(CompoundButton buttonView,
        			boolean isChecked) {
        		if(isChecked){
        			rd.setButtonDrawable(idRed);
        		}
        		else {
        			rd.setButtonDrawable(idGreen);		
        		}
        		speedSelectorListener.onSpeedChange(speedRadioButton);
        	}
        	 
        });

        radioGroup.addView(rd);
	}
	
	public void setSpeedSelectorListener(SpeedSelectorListener in_speedSelectorListener){
		speedSelectorListener = in_speedSelectorListener;
	}
	

}
